package com.file.server.controller;

import com.file.server.bean.FileInfo;
import com.file.server.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author zl
 * @date 2022/2/18 19:55
 */
@CrossOrigin
@RequestMapping("file")
@RestController
@Api(tags="文件管理")
public class FileController {

    private FileService fileService;

    @Autowired
    public void setFileService(FileService fileService){
        this.fileService = fileService;
    }

    /**
     * 根据uuid获取文件信息
     * @param newName 文件的uuid
     * @return 文件的元数据
     */
    @ApiOperation(value = "根据uuid获取文件信息")
    @GetMapping("get/{uuid}")
    public Map<String, FileInfo> getFileData(@PathVariable("uuid") String newName){
        return fileService.getFileData(newName);
    }

    /**
     * 上传文件
     * @param file 上传的文件
     * @return 返回uuid
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("upload")
    public Map<String, String> uploadFile(@RequestParam() MultipartFile file){
        return fileService.uploadFile(file);
    }

    /**
     * 根据uuid下载文件
     * @param newName 文件名称
     */
    @ApiOperation(value = "根据uuid下载文件a")
    @GetMapping("download/{uuid}")
    public void downloadFile(@PathVariable("uuid") String newName,HttpServletResponse rs){
        fileService.downloadFile(newName,rs);
    }
}
